package yzrilyzr.simplesynth.app;
import android.os.Build;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import yzrilyzr.ui.MyOscilloscopeView;
import yzrilyzr.util.AndroidUtil;
import yzrilyzr.util.Logger;
import yzrilyzr.util.Util;

public class AppInstance{
	static final ArrayList<String> name=new ArrayList<>();
	static final HashMap<String,File> map=new HashMap<>();
	public static int sampleRate=44100;
	public static MyOscilloscopeView osc;
	public static float AFreq=440;
	public static float detune=0.01f;
	public static float chorusDepth=30f;
	public static float chorusRate1=0.02f;
	public static float chorusRate2=0.01f;
	public static float chorusRatio=1f;
	public static float chorusFeedback=0.0f;
	public static float reverbDamper=0.2f;
	public static float reverbRoomSize=0.84f;
	public static float reverbWetRatio=0.2f;
	public static boolean sustain=false, limit=true;
	public static int currentPlay=-1;
	public static String sendTo="20";
	public static String mainExpr="";
	public static String replaceInst="";
	//	public static ReplaceableInstrument replaceableInstrument=new ReplaceableInstrument(
	//			new SimpleMidiInstrument());
	public static int keyBoardSendToChannel=20;
	static long nextPlay=0;
	static{
		System.loadLibrary("yzrutil");
		System.loadLibrary("SimpleSynth");
		System.loadLibrary("oboe");
		System.loadLibrary("nativelib");
		if(NativePlayer.init(Build.VERSION.SDK_INT)!=0)
			throw new RuntimeException("Audio open failed");
	}
	public static void next(){
		long curTime=System.currentTimeMillis();
		if(!NativePlayer.hasData()&&!name.isEmpty()&currentPlay>=0&&curTime-nextPlay>500){
			nextPlay=curTime;
			try{
				NativePlayer.reset();
				byte[] b=Util.read(new FileInputStream(map.get(name.get(currentPlay))));
				NativePlayer.playBytes(b,0);
				setChannelArgs();
			}catch(Exception e){
				Logger.getDef().e(e);
			}
			currentPlay++;
			if(currentPlay>=name.size()) currentPlay=0;
		}
	}
	public static void setChannelArgs(){
		NativePlayer.setUseLimiter(limit);
		for(int i=0;i<16;i++){
			NativePlayer.setAFrequency(AFreq);
			NativePlayer.setDetune(i,detune);
			NativePlayer.setSustain(i,sustain);
			NativePlayer.setChorusRate(i,0,chorusRate1);
			NativePlayer.setChorusRate(i,1,chorusRate2);
			for(int j=0;j<2;j++){
				NativePlayer.setDelayerRoomSize(i,j,reverbRoomSize);
				NativePlayer.setDelayerDamper(i,j,reverbDamper);
				NativePlayer.setDelayerRatio(i,j,reverbWetRatio);
				NativePlayer.setChorusDepth(i,j,chorusDepth);
				NativePlayer.setChorusFeedback(i,j,chorusFeedback);
				NativePlayer.setChorusRatio(i,j,chorusRatio);
			}
		}
		NativePlayer.setSustain(9,true);
		NativePlayer.setChorusRatio(9,0,0);
		NativePlayer.setChorusRatio(9,1,0);
		NativePlayer.setDelayerRatio(9,0,0);
		NativePlayer.setDelayerRatio(9,1,0);
	}
	public static void initPlaylist(){
		if(!name.isEmpty()) return;
		try{
			File dir=AndroidUtil.ctx.getExternalFilesDir("midi");
			if(dir==null) dir=new File(
					Environment.getExternalStorageDirectory()+"/Android/data/"+AndroidUtil.ctx.getPackageName()+"/midi");
			if(!dir.exists()) dir.mkdirs();
			addFile(dir);
			Collections.sort(name,new Comparator<String>(){
				@Override
				public int compare(String o1,String o2){
					return map.get(o1).getAbsolutePath().compareTo(map.get(o2).getAbsolutePath());
				}
			});
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	private static void addFile(File f){
		if(f.isDirectory()){
			File[] fs=f.listFiles();
			for(File a: fs){
				addFile(a);
			}
		}
		else if(f.isFile()){
			String k=f.getName();
			if(!k.toLowerCase().endsWith("mid")) return;
			name.add(k);
			map.put(k,f);
		}
	}
}
